package intellitor.sqlparser.web.ddl.action;

import intellitor.sqlparser.ddl.exception.BaseException;
import intellitor.sqlparser.web.commons.base.BaseAction;
import intellitor.sqlparser.web.commons.cnst.SysCodeConst;
import intellitor.sqlparser.web.ddl.bean.DdlMainBean;
import intellitor.sqlparser.web.ddl.service.DdlMainService;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;
import org.json.JSONObject;


/**
 * MAIN
 * SQL自动化主页面
 * SQL自动化工具请求处理Action
 * 主要负责文件上传，解析文件，生成SQL，执行SQL
 * @author DevilSoul
 * @since 2015-10-29
 * @version 1.0.0
 */
public class DdlMainAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static final Logger LOG = Logger.getLogger(DdlMainAction.class);
	
	//上传文件列表
	private List<File> ddlFile;
	//上传文件类型列表
	private List<String> ddlFileContentType;
	//上传文件名列表
	private List<String> ddlFileFileName;
	private DdlMainService ddlMainService;
	
	
	/**
	 * SQL自动化工具页面
	 * 选择文件按钮
	 * DDL文件上传，解析，返回结果
	 * @return
	 * @throws Exception
	 */
	public String ddlFileUpload() throws Exception{
		LOG.info("进入ddlFileUpload");
		//返回前台页面的JSON
		JSONObject json = new JSONObject();
		try {
			DdlMainBean bean = new DdlMainBean();
			bean.setDdlFile(ddlFile.get(0));
			bean.setDdlFileType(ddlFileContentType.get(0));
			ddlMainService.ddlFileUpload(bean);
			json.put(SysCodeConst.RST_KEY, SysCodeConst.SUC_CODE);
			json.put(SysCodeConst.MSG_KEY, "处理成功！");
			json.put("sql", bean.getSql());
		} catch (BaseException e){
			LOG.info(e.getMessage());
			json.put(SysCodeConst.RST_KEY, SysCodeConst.ERR_CODE);
			json.put(SysCodeConst.MSG_KEY, e.getMessage());
		} catch (Exception e) {
			LOG.error(e,e);
			json.put(SysCodeConst.RST_KEY, SysCodeConst.ERR_CODE);
			json.put(SysCodeConst.MSG_KEY, "系统异常！");
		}finally{
			LOG.info("离开ddlFileUpload");
		}
		this.writerToClient(json.toString());
		return null;
	}


	public List<File> getDdlFile() {
		return ddlFile;
	}


	public void setDdlFile(List<File> ddlFile) {
		this.ddlFile = ddlFile;
	}


	public List<String> getDdlFileContentType() {
		return ddlFileContentType;
	}


	public void setDdlFileContentType(List<String> ddlFileContentType) {
		this.ddlFileContentType = ddlFileContentType;
	}


	public List<String> getDdlFileFileName() {
		return ddlFileFileName;
	}


	public void setDdlFileFileName(List<String> ddlFileFileName) {
		this.ddlFileFileName = ddlFileFileName;
	}


	public DdlMainService getDdlMainService() {
		return ddlMainService;
	}


	public void setDdlMainService(DdlMainService ddlMainService) {
		this.ddlMainService = ddlMainService;
	}

}
